<?php
class isp_inventori_to_2_model extends MY_Model {
	var $model = 'isp_inventori_peminjaman';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	a.id_peminjaman,a.kode_barang,a.nama_barang,
							TO_CHAR(a.tgl_dipinjam,'DD/MM/YYYY') tgl_dipinjam,
							a.kode_kontrak,a.status,a.keterangan,a.jumlah,a.kode_customer,a.nama_customer,b.banyak
					FROM	isp_inventori_peminjaman a 
					LEFT JOIN isp_inventori b ON a.kode_barang = b.kode_inventori 
					WHERE	(UPPER(a.kode_barang) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.nama_barang) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.id_peminjaman' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}

	function GetJSON2()
	{
		$ls_sql	= "	SELECT	*
					FROM	isp_inventori
					WHERE	(UPPER(kode_inventori) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_inventori) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'kode_inventori' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));		
		return json_encode( $this->paging($ls_sql) );
	}

	function GetJSON3()
	{
		$jenis = "dedicated";
		$ls_sql	= "	SELECT b.no_id,b.kode_kontrak, a.kode_customer, a.nama as nama_customer,b.harga,c.bandwidth,b.nama_jenis_barang 
					FROM ISP_CUSTOMER a 
					LEFT JOIN ISP_CLIENT_KONTRAK b on a.kode_customer = b.kode_customer 
					LEFT JOIN ISP_PRODUK c on b.kode_jenis_barang = c.id_produk 
					WHERE lower(a.status) = lower('Berlangganan') 
					AND lower(b.nama_jenis_barang) LIKE '$jenis%' AND b.kode_kontrak is not null 
					AND (UPPER(a.kode_customer) 
					LIKE '%".strtoupper(post('keyword'))."%' OR UPPER(a.nama) LIKE '%".strtoupper(post('keyword'))."%') ORDER BY ".(post('sort')=='' ? 'a.kode_customer' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));		
		//echo $ls_sql;
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('isp_inventori_peminjaman', $arr_data);
	}

	function UpdateStock(){
		$this->db->update('isp_inventori');
		//echo $this->db->last_query();
	}

	function UpdateStockPeminjaman(){
		$this->db->update('isp_inventori_peminjaman');
		//echo $this->db->last_query();
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('isp_inventori_peminjaman', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_kode_inventori_p){
		$result = $this->db->get_where('isp_inventori_peminjaman', array('id_peminjaman' => $as_kode_inventori_p));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_kode_inventori_p)
	{
		$result = $this->db->get_where('isp_inventori_peminjaman', array('id_peminjaman' => $as_kode_inventori_p));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM isp_inventori_peminjaman WHERE id_peminjaman = '".$arr_rule['id_peminjaman']."'"))<=0)
			{
				return $this->error_delete.' [id: '.$arr_rule['id_peminjaman']."]<br />";
			}else{
				$this->db->delete('isp_inventori_peminjaman', array('id_peminjaman' => $arr_rule['id_peminjaman']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>